package znbkTopicType.resResponse;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lancooznbk.R;

import org.apache.commons.lang3.StringEscapeUtils;

import lancooControl.Control;
import lancooControl.CustomEditText;
import znbkTestPaperInfor.ZnbkPaperDef;

import static znbkTestPaperBj.activity.NotePaperActivity.mEntityBj;
import static znbkTestPaperRdtz.activity.HeatPointExtensionPaperActivity.mEntityRdtz;
import static znbkTestPaperSc.activity.CollectionPaperActivity.mEntitySc;

/**
 * Created by Administrator on 2018\4\25 0025.
 */

public class ZnbkResponseResStlJdtAdapter extends BaseAdapter

{

    private LayoutInflater mInflater;
    private Context mContext;
    //当前试题数量
    private int SumItem;
    //当前试题在当前试卷的ID
    private int PaperID;
    //当前问题小题所在大题的ID
    private int OptionID;
    private int ModelFrom;//试卷来源

    private int tvBlack;
    private int tvBlue;
    private int tvWhite;
    private int tvGreen;
    private int tvRed;


    private ViewHolder holder = null;
    private class ViewHolder {

        WebView mWvItemContent;
        CustomEditText mEdtStuAnswer;//输入区
        LinearLayout mLayoutCheckAnalysis;//答案回看解析区
        TextView TvAnswerAmount;//小题总数

        LinearLayout mLayoutStuAnswer;//我的答案显示区
        LinearLayout mLayoutTvStuAnswer;//我的答案文字显示
        LinearLayout mLayoutAnswer;//正确答案显示区
        LinearLayout mLayoutTvAnswer;//正确答案文字显示
        LinearLayout mLayoutStuAssess;//自评显示区
        TextView mTvStuAssess;//自评文字显示
        LinearLayout mLayoutAnalysis;//解析显示区
        TextView mTvAnalysis;//解析文字显示

        private ViewHolder(View convertView) {
            mWvItemContent = (WebView) convertView.findViewById(R.id.wv_itemContent);
            mEdtStuAnswer = (CustomEditText) convertView.findViewById(R.id.edt_stuAnswer);
            mLayoutCheckAnalysis = (LinearLayout) convertView.findViewById(R.id.layout_checkAnalysis);
            TvAnswerAmount = (TextView) convertView.findViewById(R.id.tv_answerAmount);

            mLayoutStuAnswer = (LinearLayout) convertView.findViewById(R.id.layout_stuAnswer);
            mLayoutTvStuAnswer = (LinearLayout) convertView.findViewById(R.id.layout_tvStuAnswer);
            mLayoutAnswer = (LinearLayout) convertView.findViewById(R.id.layout_answer);
            mLayoutTvAnswer = (LinearLayout) convertView.findViewById(R.id.layout_tvAnswer);
            mLayoutStuAssess = (LinearLayout) convertView.findViewById(R.id.layout_stuAssess);
            mTvStuAssess = (TextView) convertView.findViewById(R.id.tv_stuAssess);
            mLayoutAnalysis = (LinearLayout) convertView.findViewById(R.id.layout_analysis);
            mTvAnalysis = (TextView) convertView.findViewById(R.id.tv_analysis);

        }

    }

    /*
   * 获取小题选项信息
   * */
    private String SubChildAsk;//小题题干信息
    private String GenreName;//题材名称
    private String TypeName;//题型名称
    private String SubChildAnswer;//正确答案
    private String[] smallSubChildAnswer;
    private String SubChildAnalysis;//答案解析
    private int TotalItemCount;//小题数量
    private int ChildIndex;//小题编号
    public ZnbkResponseResStlJdtAdapter(Context context, int sumItem, int paperID, int optionID, int modelFrom) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.SumItem = sumItem;
        this.PaperID = paperID;
        this.OptionID = optionID;
        this.ModelFrom = modelFrom;
    }

    @Override
    public int getCount() {
        return SumItem;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.znbk_adapter_response_res_jd_item,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        /*
        * 获取作文题用户相关信息
        * */
        getInfor();

        /*
        * 显示题干信息
        * */
        showContent();

        /*
        * 显示用户回看解析相关信息
        */
        showAnalysis();

        return convertView;
    }


    /*
    * 获取作文题用户相关信息
    * */
    private void  getInfor() {

        tvBlack = mContext.getResources().getColor(R.color.black);
        tvBlue = mContext.getResources().getColor(R.color.blue);
        tvWhite = mContext.getResources().getColor(R.color.white);
        tvGreen = mContext.getResources().getColor(R.color.green);
        tvRed = mContext.getResources().getColor(R.color.red);

        if(ModelFrom== ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION) {

                    /*
                    * 题材名称：GenreName
                    * */
                    GenreName = mEntityRdtz.getResFromTK().getGenreName();
                    /*
                    * 题型名称：TypeName
                    * */
                    TypeName = mEntityRdtz.getResFromTK().getTypeName();
                    /*
                    * 小题数量
                    * */
                    TotalItemCount = mEntityRdtz.getResFromTK().getTotalItemCount();
                    /*
                    * 题干：SubChildAsk
                    * */
                    SubChildAsk = mEntityRdtz.getResFromTK().getChildList().get(OptionID).getSubChildList().get(0).getSubChildAsk();
                    /*
                    * 答案：SubChildAnswer
                    * 小题答案：smallSubChildAnswer[i]
                    * */
                    SubChildAnswer = mEntityRdtz.getResFromTK().getChildList().get(OptionID).getSubChildList().get(0).getSubChildAnswer();
                    /*
                    * 小题解析：SubChildAnalysis
                    * */
                    SubChildAnalysis = mEntityRdtz.getResFromTK().getChildList().get(OptionID).getSubChildList().get(0).getSubChildAnalyze();

                    /*
                    * 小题编号：ChildIndex
                    * */
                    ChildIndex = mEntityRdtz.getResFromTK().getChildList().get(OptionID).getChildIndex();



        }else if(ModelFrom==ZnbkPaperDef.FROM_COLLECTION){

                    /*
                    * 题材名称：GenreName
                    * */
                    GenreName = mEntitySc.getResDetail().getGenreName();
                    /*
                    * 题型名称：TypeName
                    * */
                    TypeName = mEntitySc.getResDetail().getTypeName();
                    /*
                    * 小题数量
                    * */
                    TotalItemCount = mEntitySc.getResDetail().getTotalItemCount();
                    /*
                    * 题干：SubChildAsk
                    * */
                    SubChildAsk = mEntitySc.getResDetail().getChildList().get(OptionID).getSubChildList().get(0).getSubChildAsk();
                    /*
                    * 答案：SubChildAnswer
                    * 小题答案：smallSubChildAnswer[i]
                    * */
                    SubChildAnswer = mEntitySc.getResDetail().getChildList().get(OptionID).getSubChildList().get(0).getSubChildAnswer();
                    /*
                    * 小题解析：SubChildAnalysis
                    * */
                    SubChildAnalysis = mEntitySc.getResDetail().getChildList().get(OptionID).getSubChildList().get(0).getSubChildAnalyze();
/*
                    * 小题编号：ChildIndex
                    * */
                    ChildIndex = mEntitySc.getResDetail().getChildList().get(OptionID).getChildIndex();


        }else if(ModelFrom==ZnbkPaperDef.FROM_NOTE){

                    /*
                    * 题材名称：GenreName
                    * */
                    GenreName = mEntityBj.getModelTKQue().getGenreName();
                    /*
                    * 题型名称：TypeName
                    * */
                    TypeName = mEntityBj.getModelTKQue().getTypeName();
                    /*
                    * 小题数量
                    * */
                    TotalItemCount = mEntityBj.getModelTKQue().getTotalItemCount();
                    /*
                    * 题干：SubChildAsk
                    * */
                    SubChildAsk = mEntityBj.getModelTKQue().getChildList().get(OptionID).getSubChildList().get(0).getSubChildAsk();
                    /*
                    * 答案：SubChildAnswer
                    * 小题答案：smallSubChildAnswer[i]
                    * */
                    SubChildAnswer = mEntityBj.getModelTKQue().getChildList().get(OptionID).getSubChildList().get(0).getSubChildAnswer();
                    /*
                    * 小题解析：SubChildAnalysis
                    * */
                    SubChildAnalysis = mEntityBj.getModelTKQue().getChildList().get(OptionID).getSubChildList().get(0).getSubChildAnalyze();

                    /*
                    * 小题编号：ChildIndex
                    * */
                    ChildIndex = mEntityBj.getModelTKQue().getChildList().get(OptionID).getChildIndex();

        }

    }

    /*
   * 获取题目题干信息
   * */
    private void showContent(){
        holder.TvAnswerAmount.setText("本题包含"+TotalItemCount+"个小题");
        holder.mWvItemContent.loadDataWithBaseURL("about:blank", String.valueOf(ChildIndex)+"、"+SubChildAsk, "text/html", "utf-8", null);
        holder.mWvItemContent.getSettings().setDefaultFontSize(18);
    }


    private TextView mTvAnswer;
    /*
    * 显示试题解析回看信息
    * */
    private void showAnalysis(){


        //隐藏作答区域
        holder.mEdtStuAnswer.setVisibility(View.GONE);
        //显示题目回看解析内容
        holder.mLayoutCheckAnalysis.setVisibility(View.VISIBLE);
        //隐藏我的答案
        holder.mLayoutStuAnswer.setVisibility(View.GONE);
        //显示参考答案
        mTvAnswer = new TextView(mContext);
        mTvAnswer.setText(Html.fromHtml(SubChildAnswer));
        mTvAnswer.setTextSize(16);
        mTvAnswer.setTextColor(tvGreen);
        holder.mLayoutTvAnswer.addView(mTvAnswer);
        //隐藏自评
        holder.mLayoutStuAssess.setVisibility(View.GONE);
        //显示答案解析
        if (!SubChildAnalysis.equals("")) {
            holder.mTvAnalysis.setText(Html.fromHtml(SubChildAnalysis));
        } else {
            holder.mTvAnalysis.setText(R.string.znbk_no_analysis);
        }

    }

}